#! C:\Perl\bin\perl.exe

# Copyright (c) 2010, College Center for Library Automation

# See the included Copyright.txt file for full license

# Login once for all colleges. Ask for report for each college.

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use Date::Calc qw(Decode_Month Add_Delta_YM Today);
use Text::CSV;
use HTML::Entities;	# gives the html decode function to decode &#99; characters

use avsCommon;

my ($content, $response, $request, $url);
my ($viewstate);
my ($runyear, $runmonth, $runday);

# create web user agent and cookie jar
my $ua =  new LWP::UserAgent(keep_alive=>1);
$ua->cookie_jar( {} );

dbsetup();

if ($begyear < 2008 || $begyear == 2008 && $begmonth < 12) {
	warn "#info: facts on file changed their website 12/2008, use get_facts_pre12_2009.pl to get previous data";
	$begyear = 2008;
	$begmonth = 12;
}

do {
	if ($username) {
		getLogin();

		($runyear, $runmonth, $runday) = @begdate;

		# run once for each year as that is the most we can ask for in a report. typicall only run for one month.
		while ($runyear < $endyear || $runyear == $endyear && $runmonth <= $endmonth) {
			getStats ();
			$runyear++;
		}

		doLogout();
	}
} while ($sthlogin->fetch);

dbshutdown();

exit ();

sub getLogin {
	$url = 'http://www.fofweb.com/Reporting/Login.aspx';
	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET login $url : " . $response->status_line;
	};

	$content=$response->decoded_content;
	$content =~ /VIEWSTATE\" value\=\"(?<viewstate>.*?)\"/sm;
	$viewstate = urlencode($+{viewstate});
	
	# POST login request using viewstate just returned. sets cookie with session
	$request = HTTP::Request->new(POST => $url);
	$request->content_type('application/x-www-form-urlencoded');
	$request->content("__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=$viewstate" .
			"&UserName=$username&Password=$password&LogInBtn.x=0&LogInBtn.y=0");
	$response = $ua->request($request);
	
	if (!$response->is_success && substr($response->status_line,0,3) ne '302') {
		die "#error:$0: POST login $url" . $response->status_line;
	};
};

sub getStats {
	my ($runendyear, $runendmonth, $runendday) = 	Add_Delta_YM($runyear, $runmonth, 1, 1, 0);	# try for the next 12 months
	
	# but don't got beyond end date
	if ($runendyear > $endyear || $runendyear == $endyear && $runendmonth > $endmonth) {
		$runendyear = $endyear;
		$runendmonth = $endmonth;
	};

	$url = "http://www.fofweb.com/Reporting/CounterIFrame.aspx?DateFrom=$begmonth/1/$begyear&DateTo=$runendmonth/1/$runendyear&DataSelection=&IPSelection=";
	
	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET form $url : " . $response->status_line;
	};

	$content=$response->decoded_content;

	# save contents for possible debugging
	savecontent('Facts', $collegeid, $content);

	# prepare the HTML for extraction 
	$content =~ s/\s\s//g; # remove spaces/cr lf
	$content =~ s/<\/tr>/\n/g; # add cr on table row breaks
	$content =~ s/<\/td>/~/g; # add splitable character ~
	$content =~ s/<.*?>//g; # remove <html>
	$content =~ s/ ~/~/g; # remove extra space in datasources
	$content =~ s/\t//g; # remove tabs
	
	if ($content =~ /DataProduct~(?<data>.*?)\n~\n/s) {

		my @contents = split("\n",$+{data});

		# setup periods/dates expected in data stream
		my @periods = ('','');	# number of empty columns before actual data
		my $begperiodndx = 2;		# based on number of empty columns.
		my $endperiodndx = 2;
		my $begperiod = myperiod($runyear, $runmonth);
		my $endperiod = myperiod($runendyear, $runendmonth);

		# grab the dates off of the header lines.
		my $months = shift(@contents);

		# get months that is between the begining and ending dates from requested data
		while ($months =~ /(?<month>\d+)\/(?<year>\d{4})/g) {
			my $period = myperiod($+{year},$+{month});
			$begperiodndx++ if ($period lt $begperiod);
			$endperiodndx++ if ($period lt $endperiod);
			push @periods, $period;
		}
		$begperiodndx-- if $begperiodndx == 14;	# prevent multiyear problem
		$endperiodndx-- if $endperiodndx == 14;	# prevent multiyear problem

		# go through each line of data. searches and sessions are on seperate lines so
		foreach my $contents (@contents) {
			my @columns = split('~',$contents);

			my $datasource = decode_entities($columns[0]);
			my $type = $columns[1] eq 'Sessions' ? 1 : 2;
			if ($datasource ne '') {
				foreach my $i ($begperiodndx..$endperiodndx) {
					print "$vendorid $collegeid $periods[$i] '$datasource' $columns[$i] $type\n";
					$sthstats->execute($vendorid, $collegeid, $datasource, $periods[$i], $columns[$i], $type)
						or die "#error: sthstats execute $type failed";
				}
			}
		}
	}
}

sub doLogout {
	# get viewstate and eventvalidation for report form
	$url = 'http://www.fofweb.com/Reporting/Login.aspx?exit=1';
	$response = $ua->request(GET $url);
}